
#include <Wire.h>
#include <Adafruit_GFX_STM32.h>
#include <Adafruit_SSD1306_STM32.h>
TwoWire wire1(1);
//TwoWire Wire1(1, I2C_FAST_MODE);
#define display_SCREEN_WIDTH  128
#define display_SCREEN_HEIGHT 64
#define display_OLED_RESET    PA0
Adafruit_SSD1306_STM32 display(display_SCREEN_WIDTH, display_SCREEN_HEIGHT, &wire1, display_OLED_RESET);
//字体：楷体  字号：16px  显示文字：牛
/*
--------------------------------
|                              |
|              0 0 0           |
|              0 0 0           |
|        0 0 0   0             |
|        0 0 0   0 0 0 0       |
|        0 0 0 0 0 0 0 0       |
|      0 0       0             |
|      0         0 0 0 0 0 0 0 |
|  0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
|  0 0 0 0 0     0             |
|                0             |
|                0             |
|                0             |
|                0             |
|                0             |
|                0             |
--------------------------------
*/
static const unsigned char PROGMEM bitmap1[32] = {0x00,0x00,0x01,0xC0,0x01,0xC0,0x0E,0x80,0x0E,0xF0,0x0F,0xF0,0x18,0x80,0x10,0xFF,0x7F,0xFF,0x7C,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80};
//字体：楷体  字号：16px  显示文字：顿
/*
--------------------------------
|                              |
|                              |
|      0 0         0 0 0 0 0 0 |
|      0 0   0 0 0 0 0 0 0     |
|      0 0 0 0     0 0 0 0     |
|    0 0 0 0 0 0 0 0 0 0 0 0   |
|  0 0 0 0 0 0 0 0 0 0   0 0   |
|  0 0 0 0 0 0   0 0 0   0     |
|  0 0 0 0 0 0   0   0   0 0   |
|  0 0 0 0   0 0 0 0 0   0 0   |
|      0 0 0 0 0 0 0 0   0 0   |
|      0 0 0     0 0 0 0 0     |
|      0 0       0 0   0 0 0   |
|      0     0 0 0       0 0   |
|            0 0         0 0   |
|                              |
--------------------------------
*/
static const unsigned char PROGMEM bitmap2[32] = {0x00,0x00,0x00,0x00,0x18,0x7E,0x1B,0xF8,0x1E,0x78,0x3F,0xFC,0x7F,0xEC,0x7E,0xE8,0x7E,0xAC,0x7B,0xEC,0x1F,0xEC,0x1C,0xF8,0x18,0xDC,0x13,0x8C,0x03,0x0C,0x00,0x00};
//字体：楷体  字号：16px  显示文字：摆
/*
--------------------------------
|                              |
|      0 0                     |
|      0 0 0         0 0 0 0 0 |
|        0 0 0 0 0 0 0 0 0 0 0 |
|        0     0 0 0   0 0 0   |
|      0 0 0 0 0 0 0 0 0 0 0   |
|  0 0 0 0 0 0 0 0 0 0         |
|        0 0 0     0 0 0 0 0   |
|      0 0 0   0 0 0 0 0 0 0   |
|  0 0 0 0         0 0 0 0 0 0 |
|0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
|  0     0         0 0 0 0     |
|    0 0 0       0 0   0 0 0   |
|    0 0 0     0 0 0 0 0 0 0   |
|      0 0     0 0         0   |
|                              |
--------------------------------
*/
static const unsigned char PROGMEM bitmap3[32] = {0x00,0x00,0x18,0x00,0x1C,0x3E,0x0F,0xFE,0x09,0xDC,0x1F,0xFC,0x7F,0xE0,0x0E,0x7C,0x1D,0xFC,0x78,0x7E,0xFF,0xFF,0x48,0x78,0x38,0xDC,0x39,0xFC,0x19,0x84,0x00,0x00};
//字体：楷体  字号：16px  显示文字：演
/*
--------------------------------
|                              |
|                0 0           |
|    0 0 0       0 0 0         |
|      0 0 0 0     0 0 0 0 0 0 |
|      0 0 0 0 0 0 0 0 0 0 0 0 |
|          0 0   0 0 0   0     |
|  0 0 0 0 0   0 0 0 0         |
|    0 0   0 0 0 0 0 0 0 0     |
|        0 0 0 0 0 0 0 0 0     |
|      0 0   0 0 0 0 0 0 0     |
|    0 0     0 0 0 0 0 0       |
|  0 0 0     0 0 0 0 0 0       |
|  0 0 0     0 0 0   0 0 0     |
|    0     0 0 0     0 0 0     |
|        0 0 0         0 0 0   |
|                              |
--------------------------------
*/
static const unsigned char PROGMEM bitmap4[32] = {0x00,0x00,0x00,0xC0,0x38,0xE0,0x1E,0x7E,0x1F,0xFE,0x06,0xE8,0x7D,0xE0,0x37,0xF8,0x0F,0xF8,0x1B,0xF8,0x33,0xF0,0x73,0xF0,0x73,0xB8,0x27,0x38,0x0E,0x1C,0x00,0x00};
//字体：楷体  字号：16px  显示文字：示
/*
--------------------------------
|                              |
|                              |
|                0 0 0 0       |
|          0 0 0 0 0 0 0       |
|          0 0 0 0             |
|                              |
|          0 0 0 0 0 0 0 0 0 0 |
|  0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
|              0 0             |
|        0     0 0   0 0 0     |
|      0 0     0 0     0 0 0   |
|      0 0     0 0     0 0 0   |
|      0 0     0 0         0   |
|            0 0 0             |
|              0 0             |
|              0               |
--------------------------------
*/
static const unsigned char PROGMEM bitmap5[32] = {0x00,0x00,0x00,0x00,0x00,0xF0,0x07,0xF0,0x07,0x80,0x00,0x00,0x07,0xFE,0x7F,0xFE,0x01,0x80,0x09,0xB8,0x19,0x9C,0x19,0x9C,0x19,0x84,0x03,0x80,0x01,0x80,0x01,0x00};
volatile int damping_factor;

void draw_data(byte j) {
  display.clearDisplay();
  display.drawFastHLine(0, 0, 127, WHITE);
  for (int i = 2; i <= 4; i = i + (1)) {
    display.drawFastVLine((15 + i * 16), 0, 40, WHITE);
    display.fillCircle((15 + i * 16), 48, 8, WHITE);
  }
  if (j <= 90) {
    display.drawFastVLine((15 + 1 * 16), 0, 40, WHITE);
    display.fillCircle((15 + 1 * 16), 48, 8, WHITE);
    display.drawLine(95, 0, (95 + 40 * cos(j / 180.0 * 3.14159)), (0 + 48 * sin(j / 180.0 * 3.14159)), WHITE);
    display.fillCircle((95 + 40 * cos(j / 180.0 * 3.14159)), (0 + 48 * sin(j / 180.0 * 3.14159)), 8, WHITE);

  } else {
    display.drawFastVLine((15 + 5 * 16), 0, 40, WHITE);
    display.fillCircle((15 + 5 * 16), 48, 8, WHITE);
    display.drawLine(31, 0, (31 + 40 * cos(j / 180.0 * 3.14159)), (0 + 48 * sin(j / 180.0 * 3.14159)), WHITE);
    display.fillCircle((31 + 40 * cos(j / 180.0 * 3.14159)), (0 + 48 * sin(j / 180.0 * 3.14159)), 8, WHITE);

  }
  display.display();
}

void setup(){
  afio_remap(AFIO_REMAP_I2C1);
  gpio_set_mode(GPIOB, 9, GPIO_AF_OUTPUT_OD);
  gpio_set_mode(GPIOB, 8, GPIO_AF_OUTPUT_OD);
  
  display.begin(SSD1306_SWITCHCAPVCC,0x3C);
  damping_factor = 1;
  display.clearDisplay();
  display.drawBitmap(40, 16, bitmap1, 16, 16, WHITE);
  display.drawBitmap(56, 16, bitmap2, 16, 16, WHITE);
  display.drawBitmap(72, 16, bitmap3, 16, 16, WHITE);
  display.drawBitmap(48, 35, bitmap4, 16, 16, WHITE);
  display.drawBitmap(64, 35, bitmap5, 16, 16, WHITE);
  display.display();
  delay(5000);
}

void loop(){
  damping_factor = 1;
  for (int l = 1; l <= 30; l = l + (2 * damping_factor)) {
    for (int k = (60 + l); k <= ((120 - l) - damping_factor); k = k + (1)) {
      draw_data(k);
    }
    for (int k = ((120 - l) - damping_factor); k >= ((60 + l) + damping_factor); k = k + (-1)) {
      draw_data(k);
    }
  }
  delay(5000);

}